package GUI.GameBoard;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;

import GUI.PlayerPanel.PlayerButton;
import Utils.Utils;

//VS4E -- DO NOT REMOVE THIS LINE!
/**
 * @author Avia Tam
 * @author Assaf Miron
 *
 */
public class PlayersSquarePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel pnlPlayers;
	private HousePanel pnlHouse;

	public PlayersSquarePanel() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new BorderLayout());
		// Make invisible
		setOpaque(false);
		// Set Preferred size
		setPreferredSize(new Dimension(110, 100));
		// Add the Houses Panel
		add(getHousePanel(),BorderLayout.NORTH);
		// Add the Players Panel
		add(getPlayersPanel(), BorderLayout.CENTER);
	}

	/**
	 * @return The Houses Panel
	 */
	private HousePanel getHousePanel() {
		if(pnlHouse == null)
		{
			pnlHouse = new HousePanel();
		}
		return pnlHouse;
	}

	/**
	 * Create the Player Panel
	 * @return The Players Panel
	 */
	private JPanel getPlayersPanel() {
		if (pnlPlayers == null) 
		{
			pnlPlayers = new JPanel();
			pnlPlayers.setOpaque(false);
			pnlPlayers.setLayout(new GridBagLayout());
			GridBagConstraints pnlplayerconstrain = new GridBagConstraints();
			// Create 9 Empty Panels in the Players Panel (Save seats for 6 Players, 3 Extra)
			for(int i = 0; i < 3; i++)
			{
				for(int j = 0; j < 3; j++)
				{
					pnlplayerconstrain.gridx = j;
					pnlplayerconstrain.gridy = i;
					pnlplayerconstrain.fill = GridBagConstraints.BOTH;
					pnlplayerconstrain.weightx = 0.5;
					pnlplayerconstrain.weighty = 0.5;
					pnlplayerconstrain.ipadx = 2;
					pnlplayerconstrain.ipady = 2;
					pnlPlayers.add(createEmptyPanel(),pnlplayerconstrain);
				}
			}
		}
		return pnlPlayers;
	}

	/**
	 * Creates an Empty Transparent Panel
	 * @return The New Empty Panel
	 */
	private JPanel createEmptyPanel()
	{
		JPanel ret = new JPanel();
		ret.setLayout(new BorderLayout());
		// Make transparent
		ret.setOpaque(false);
		// Set size for the Empty Panel
		ret.setMinimumSize(new Dimension(5,5));
		ret.setPreferredSize(new Dimension(10,10));
		return ret;
	}

	/**
	 * Adds a player to the Players Panel
	 * @param playerButton The Player button to add
	 */
	public void addPlayer(PlayerButton playerButton)
	{
		//get the placing number
		Integer index=playerButton.getPlacing_number();
		JPanel addTOPanel;
		// Get the Components in the Player Index
		addTOPanel = (JPanel) pnlPlayers.getComponent(index);
		// Add the Player to its Panel
		addTOPanel.add(playerButton, BorderLayout.CENTER);
		// Repaint the Change
		addTOPanel.repaint();
		// Repaint and Validate
		Utils.repaintAndValidateAll(pnlPlayers);
	}

	/**
	 * Removes a player from the Players Panel
	 * @param playerButton The Player button to remove
	 */
	public void removePlayer(PlayerButton playerButton)
	{
		// Get the placing number
		Integer index = playerButton.getPlacing_number();
		JPanel removeFROMPanel;
		removeFROMPanel = (JPanel) pnlPlayers.getComponent(index);
		removeFROMPanel.remove(playerButton);
		// Validate Removal
		removeFROMPanel.validate();
		// Repaint and Validate
		Utils.repaintAndValidateAll(pnlPlayers);
	}

	/**
	 * Adds a New House to the Houses Panel
	 * @param numOfHouses The Number of Houses Needed
	 */
	public void addHouse(Integer numOfHouses)
	{
		// Add the House to the Panel
		pnlHouse.addHouses(numOfHouses);
	}

	/**
	 * Clears all the Houses in the Houses Panel
	 */
	public void clearHouses()
	{
		// Clear the Houses
		pnlHouse.clearHouses();
	}
}
